/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.product;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.event.MethodExpressionActionListener;
import org.hutech09.jsale.bsn.ICategoryManager;
import org.hutech09.jsale.entity.Category;
import org.hutech09.jsale.entity.Product;
import org.hutech09.jsale.entity.ProductType;
import org.hutech09.jsale.web.customer.cusController;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;

/**
 *
 * @author abc
 */
public class cateController implements Serializable {

    @EJB
    private ICategoryManager categoryManager;
    private Category cate;
    private List<Product> lispro = new ArrayList<Product>();

    public List<Product> getLispro() {
        return lispro;
    }

    public void setLispro(List<Product> lispro) {
        this.lispro = lispro;
    }

    public Category getCate() {

        return cate;
    }

    public void setCate(Category cate) {
        this.cate = cate;
    }
    private List<ProductType> listProtype;
    private List<Category> listcase;

    public List<Category> getListcase() {
        this.listcase = categoryManager.getall();
        return listcase;
    }

    public void setListcase(List<Category> listcase) {
        this.listcase = categoryManager.getall();
        this.listcase = listcase;
    }

    public List<ProductType> getListProtype() {

        return listProtype;
    }

    public void setListProtype(List<ProductType> listProtype) {
        this.listProtype = listProtype;
    }

    public cateController() {
        listProtype = new ArrayList<ProductType>();
        listcase = new ArrayList<Category>();


    }
    private MenuModel model;

    public MenuModel getModel() {
        this.listcase = categoryManager.getall();
        model = new DefaultMenuModel();
        for (Category category : listcase) {
            this.listProtype = category.getListProtype();

            Submenu sub = new Submenu();
            sub.setLabel(category.getName());
            for (ProductType productType : listProtype) {
                MenuItem mit = new MenuItem();
                mit.setValue(productType.getName());
                mit.setUpdate("listproduct,message");
                ExpressionFactory factory = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
                MethodExpression methodsexpression = factory.createMethodExpression(FacesContext.getCurrentInstance().getELContext(), "#{productController.getProduct('" + productType.getId() + "')}", null, new Class[0]);
                mit.setActionExpression(methodsexpression);
                sub.getChildren().add(mit);
            }

            model.addSubmenu(sub);
        }
        return model;
    }

    public void setModel(MenuModel model) {
        this.model = model;
    }
    private MenuModel modelnoajax;

    public MenuModel getModelnoajax() {
        this.listcase = categoryManager.getall();
        modelnoajax = new DefaultMenuModel();
        for (Category category : listcase) {
            this.listProtype = category.getListProtype();

            Submenu sub = new Submenu();
            sub.setLabel(category.getName());
            for (ProductType productType : listProtype) {
                MenuItem mit = new MenuItem();
                mit.setValue(productType.getName());
                ExpressionFactory factory = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
                MethodExpression methodsexpression = factory.createMethodExpression(FacesContext.getCurrentInstance().getELContext(), "#{productController.getProduct('" + productType.getId() + "')}", null, new Class[0]);
                mit.setActionExpression(methodsexpression);
                sub.getChildren().add(mit);
            }

            modelnoajax.addSubmenu(sub);
        }
         return modelnoajax;
    }

    public void setModelnoajax(MenuModel modelnoajax) {
        this.modelnoajax = modelnoajax;
    }
    
   
    
}
